package cn.tianya.irock.core;


import java.util.Map;

import org.apache.log4j.Logger;

import cn.tianya.irock.bll.SiteManager;
import cn.tianya.irock.data.model.Site;
import cn.tianya.irock.enums.ApplicationExceptionType;
import cn.tianya.irock.enums.SystemExceptionType;
import cn.tianya.irock.exceptions.ApplicationException;
import cn.tianya.irock.exceptions.SystemException;

public class PageFactory {
//	private static Logger logger = Logger.getLogger(PageFactory.class);
//	private Properties props;
//	
//	public PageFactory(String pagesFile) throws IOException {
//		FileInputStream input = null;
//		
//		props = new Properties();
//		input = new FileInputStream(pagesFile);
//		props.load(input);
//		input.close();
//	}
//	
//	private String getQualifiedClassName(String pagename) {
//		return props.getProperty(pagename);
//	}
//	
//	public Page getPage(String pagename) {
//		Page page = null;
//		try {
//			page = (Page) Class.forName(getQualifiedClassName(pagename)).newInstance();
//		} catch (Exception ex) {
//			logger.error("Exception", ex);
//			throw new ApplicationException(ApplicationExceptionType.FileNotFound, ex.getMessage());
//		}
//		return page;
//	}
	
	private static Logger logger = Logger.getLogger(PageFactory.class);
	
	public PageFactory() {
	}
	
	private String getQualifiedClassName(String sitename, String pagename) {
		Map<String, cn.tianya.irock.data.model.Page> pages = null;
		Site site = SiteManager.findSiteByname(sitename);
		
		pages = SiteManager.getPageMap(site.getSettings().getSiteSettingsId());
		return pages.get(pagename).getRefer();
	}
	
	public Page getPage(String sitename, String pagename) {
		Page page = null;
		try {
			page = (Page) Class.forName(getQualifiedClassName(sitename, pagename)).newInstance();
		} catch (NullPointerException ex) {
			throw new ApplicationException(ApplicationExceptionType.PageNotFound);
		} catch (Exception ex) {
			throw new ApplicationException(ApplicationExceptionType.UnknowError, ex.getMessage());
		}
		return page;
	}
}
